package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Vehicle;
import models.Driver;
@Entity
@Table(name="accident")
public class Accident extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Vehicle vehicle_our; //车号
	@ManyToOne
	public Driver driver_our; //我方驾驶员
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date accidentdatetime; //事故发生时间
	
	public String reporter; //接报人
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date recorddatetime; //登记时间
	
	public String place; //事故地点
	
	public String response; //事故现场定责
	
	public String damage; //我方受损情况
	
	public String driver2; //对方驾驶员
	
	public String plate_number2; //车号
	
	public Integer xszh; //行驶证号
	
	public Integer jzh; //驾证号
	
	public String damage2; //受损情况
	
	public String story; //事故发生原因及经过
	
	public String policecomment; //事故发生所在地交警意见
	
	public String handover; //事故处理交接情况
	
	public String accidenttype; //事故性质
	
	public String death; //伤亡人数
	
	public Integer fee; //事故总支出
	
	public Integer insurance; //保险理赔
	
	public Integer companypay; //公司支付
	
	public Integer personalpay; //个人支付
	
	public Integer commpanylost; //公司损失
	
	public String comment; //备注
	public static Model.Finder<Integer, Accident> find = new Model.Finder<Integer, Accident>(Integer.class, Accident.class);

	public static Page<Accident> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Accident> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setVehicle_our(Vehicle d) {
		vehicle_our= d;
	}
	public Vehicle getVehicle_our() {
		return vehicle_our;
	}
	public void setDriver_our(Driver d) {
		driver_our= d;
	}
	public Driver getDriver_our() {
		return driver_our;
	}
	public void setAccidentdatetime(Date d) {
		accidentdatetime= d;
	}
	public Date getAccidentdatetime() {
		return accidentdatetime;
	}
	public void setReporter(String d) {
		reporter= d;
	}
	public String getReporter() {
		return reporter;
	}
	public void setRecorddatetime(Date d) {
		recorddatetime= d;
	}
	public Date getRecorddatetime() {
		return recorddatetime;
	}
	public void setPlace(String d) {
		place= d;
	}
	public String getPlace() {
		return place;
	}
	public void setResponse(String d) {
		response= d;
	}
	public String getResponse() {
		return response;
	}
	public void setDamage(String d) {
		damage= d;
	}
	public String getDamage() {
		return damage;
	}
	public void setDriver2(String d) {
		driver2= d;
	}
	public String getDriver2() {
		return driver2;
	}
	public void setPlate_number2(String d) {
		plate_number2= d;
	}
	public String getPlate_number2() {
		return plate_number2;
	}
	public void setXszh(Integer d) {
		xszh= d;
	}
	public Integer getXszh() {
		return xszh;
	}
	public void setJzh(Integer d) {
		jzh= d;
	}
	public Integer getJzh() {
		return jzh;
	}
	public void setDamage2(String d) {
		damage2= d;
	}
	public String getDamage2() {
		return damage2;
	}
	public void setStory(String d) {
		story= d;
	}
	public String getStory() {
		return story;
	}
	public void setPolicecomment(String d) {
		policecomment= d;
	}
	public String getPolicecomment() {
		return policecomment;
	}
	public void setHandover(String d) {
		handover= d;
	}
	public String getHandover() {
		return handover;
	}
	public void setAccidenttype(String d) {
		accidenttype= d;
	}
	public String getAccidenttype() {
		return accidenttype;
	}
	public void setDeath(String d) {
		death= d;
	}
	public String getDeath() {
		return death;
	}
	public void setFee(Integer d) {
		fee= d;
	}
	public Integer getFee() {
		return fee;
	}
	public void setInsurance(Integer d) {
		insurance= d;
	}
	public Integer getInsurance() {
		return insurance;
	}
	public void setCompanypay(Integer d) {
		companypay= d;
	}
	public Integer getCompanypay() {
		return companypay;
	}
	public void setPersonalpay(Integer d) {
		personalpay= d;
	}
	public Integer getPersonalpay() {
		return personalpay;
	}
	public void setCommpanylost(Integer d) {
		commpanylost= d;
	}
	public Integer getCommpanylost() {
		return commpanylost;
	}
	public void setComment(String d) {
		comment= d;
	}
	public String getComment() {
		return comment;
	}
	public static Accident findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Accident v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
